package com.four.service.impl;

import com.four.entity.Admin;
import com.four.entity.Dept;
import com.four.entity.Scheduling;
import com.four.entity.Section;
import com.four.mapper.SchedulingMapper;
import com.four.service.SchedulingService;
import com.four.vo.Result;
import com.four.vo.ScheSectionVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.bcel.internal.classfile.Code;
import org.apache.ibatis.executor.ReuseExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @projectName: appointmentsystemfour
 * @package: com.four.service.impl
 * @className: SchedulingServiceImpl
 * @author: ZZH
 * @description: TODO
 * @date: 2022/10/13 11:41
 * @version: 1.0
 */
@Service
public class SchedulingServiceImpl implements SchedulingService {

    @Autowired
    private SchedulingMapper schedulingMapper;

    //预约成功修改排班预约数量
    @Override
    public Result appointmentSubNum(Integer scheulingId) {

        //1.根据主键id查询出这条排班信息
        Scheduling scheduling = schedulingMapper.selectByPrimaryKey(scheulingId);

        //2.判断剩余数量是否大于0
        if (scheduling.getNum()>0){
            //3.剩余数量大于0,将剩余数量-1
            scheduling.setNum(scheduling.getNum() - 1);
            int i = schedulingMapper.updateByPrimaryKeySelective(scheduling);
            if(i>0){
                return new Result(2000,"预约成功");
            }
        }

        return new Result(5000,"很遗憾预约人数已满!");
    }

    //添加排班信息
    public  Result addScheduling(Scheduling scheduling){
        int i = schedulingMapper.insertSelective(scheduling);
        if (i>0){
            return new Result(2000,"添加排班信息成功");
        }
        return new Result(5000,"添加排班信息失败");
    }


    @Override
    public Result scheduling(Integer sectionid,Integer currentPage,Integer pageSize) {

        //最后的返回数据是一个map
        Map<Object, Object> map = new HashMap<>();


        //1.根据科室id查询所有的医生
        PageHelper.startPage(currentPage,pageSize);
        List<Admin> doctors = schedulingMapper.doctors(sectionid);
        //System.out.println(doctors);
        PageInfo<Admin> pageInfo = new PageInfo<>(doctors);


        //获取日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        String s = new String();

        //循环查询每个医生的未来七天的排班情况
        for (Admin doctor : doctors) {
            ArrayList<Object> dateList = new ArrayList<>();
            dateList.add(doctor);
            for (int i = 0; i < 7 ; i++) {

                //设置时间
                calendar.add(calendar.DATE,i);
                s = sdf.format(calendar.getTime());

                //根据医生id和日期查询排班信息
                Scheduling scheduling = schedulingMapper.selectByAdminIdAndDate(doctor.getId(), s);

                //判断排班信息是否为空
                if(scheduling==null){
                    Scheduling nullSche = new Scheduling();
                    nullSche.setDate(calendar.getTime());
                    nullSche.setAdminId(doctor.getId());
                    nullSche.setDoctorName(doctor.getName());
                    dateList.add(nullSche);
                }else{
                    dateList.add(scheduling);
                }
                calendar.add(calendar.DATE,-i);
            }
            map.put(doctor.getId(),dateList);
        }

        Map<String,Object> maps=new HashMap<>();
        maps.put("total",pageInfo.getTotal());
        maps.put("data",map);


        return new Result(2000, "success",maps);
    }

    @Override
    public Result findScheDept() {
        List<Dept> depts = schedulingMapper.selectScheDept();

        List<ScheSectionVo> voList = new ArrayList<>();

        //循环查询这些部门的科室
        for (Dept dept : depts) {
            ScheSectionVo scheSectionVo = new ScheSectionVo();
            List<Section> sections = schedulingMapper.selectScheSection(dept.getDeptId());
            scheSectionVo.setDept(dept);
            scheSectionVo.setSections(sections);
            voList.add(scheSectionVo);
        }

        return new Result(2000,"查询排班部门成功",voList);
    }

    @Override
    public Result menu(){
        List<Dept> depts = schedulingMapper.selectScheDept();

        //循环查询这些部门的科室
        for (Dept dept : depts) {
            List<Dept> children=new ArrayList<>();
            List<Section> sections = schedulingMapper.selectScheSection(dept.getDeptId());
            for (Section section : sections) {
                Dept childs = new Dept();
                childs.setDeptId(section.getSectionId());
                childs.setDeptName(section.getSectionName());
                children.add(childs);
            }
            dept.setChildren(children);
        }

        return new Result(2000,"查询排班部门成功",depts);
    }



}
